const path = require("path");
const _ = require("lodash");
const moment = require("moment");
const siteConfig = require("./data/SiteConfig");
require("babel-polyfill");

const postNodes = [];

function addSiblingNodes(createNodeField) {
  for (let i = 0; i < postNodes.length; i += 1) {
    const nextID = i + 1 < postNodes.length ? i + 1 : 0;
    const prevID = i - 1 > 0 ? i - 1 : postNodes.length - 1;
    const currNode = postNodes[i];
    const nextNode = postNodes[nextID];
    const prevNode = postNodes[prevID];
    createNodeField({
      node: currNode,
      name: "nextTitle",
      value: nextNode.title
    });
    
    createNodeField({
      node: currNode,
      name: "nextSlug",
      value: nextNode.id
    });
    createNodeField({
      node: currNode,
      name: "prevTitle",
      value: prevNode.title
    });
    createNodeField({
      node: currNode,
      name: "prevSlug",
      value: prevNode.id
    });
  }
}

exports.onCreateNode = ({ node, actions, getNode }) => {
  const { createNodeField } = actions;
  let slug;
  if (node.internal.type === "StrapiArticle") {
    createNodeField({ node, name: "slug", value: node.id });
    postNodes.push(node);
  }
};

exports.setFieldsOnGraphQLNodeType = ({ type, actions }) => {
  const { name } = type;
  const { createNodeField } = actions;
  if (name === "StrapiArticle") {
    addSiblingNodes(createNodeField);
  }
};

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions;

  return new Promise((resolve, reject) => {
    const postPage = path.resolve("src/templates/post.jsx");
    const tagPage = path.resolve("src/templates/tag.jsx");

    resolve(
      graphql(
        `
        query IndexQuery {
          allStrapiArticle {
            edges {
              node {
                id
                title
                content
                createdAt
                cover{id name}
                tags{id name}
              }
            }
          }
        }

        `
      ).then(result => {
        if (result.errors) {
          /* eslint no-console: "off" */
          console.log(result.errors);
          reject(result.errors);
        }

        const tagSet = new Set();
        const categorySet = new Set();
        result.data.allStrapiArticle.edges.forEach(edge => {
          if (edge.node.tags) {
            edge.node.tags.forEach(tag => {
              tagSet.add(tag.name);
            });
          }


          createPage({
            path: edge.node.id,
            component: postPage,
            context: {
              slug: edge.node.id
            }
          });
        });

        const tagList = Array.from(tagSet);
        tagList.forEach(tag => {
          createPage({
            path: `/tags/${_.kebabCase(tag)}/`,
            component: tagPage,
            context: {
              tag
            }
          });
        });


      })
    );
  });
};
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /social-share-react11/,
            use: loaders.null(),
          },
        ],
      },
    })
  }
}